(function () {
    app.controller("createCrl", ['$scope', '$rootScope', 'tsOrderSiteService', function ($scope, $rootScope, tsOrderSiteService) {
        $scope.model = {};
        var dataSource;
        $scope.save = function () {
            if ($scope.validator.validate()) {
            }
            for (var i = 0; i < dataSource.data().length; i++) {
                var item = dataSource.data()[i];
                if ($scope.model.siteId == item.siteId && !item.isSiteIdLspId && !$scope.model.operateTime) {
                    kendo.factory.notifyError("若分配给承运商，转运时间必填");
                    return false;
                }
            }
            $scope.btnSubmit.enable(false);
            tsOrderSiteService.distributionSiteOrLsp($scope.model)
                .done(function (result) {
                    if (result.success) {
                        $scope.createWindow.close();
                        kendo.factory.notifySuccess("分配成功");
                        $rootScope.reloadTsGrid();
                    } else {
                        kendo.factory.notifyError(result.message);
                    }
                }).always(function () {
                $scope.btnSubmit.enable(true);
            });

            return false;
        };

        $scope.siteOptions = {
            dataTextField: "name",
            dataValueField: "siteId",
            /*  optionLabel:"请选择",*/
            data: new kendo.data.DataSource({data: []})
        };

        $rootScope.$on("distributionSite", function (event, data) {
            $scope.model.ids = data;
            $scope.model.operateTime = new Date().format("yyyy-MM-dd HH:mm:ss");
            tsOrderSiteService.findSiteAndLsp(data[0]).done(function (result) {
                dataSource = new kendo.data.DataSource({
                    data: result
                });
                var dropdownlist = $("#site").data("kendoDropDownList");
                dropdownlist.setDataSource(dataSource);
                dropdownlist.select(0);
                dropdownlist.trigger("change");
            });

            $scope.createWindow.center().open();
        });
    }]);
})();